from flask import Flask, render_template, request, escape
import geo
from log_sys import log_request


app = Flask(__name__)


@app.route('/')
def base():
    title = '搜索POI'
    return render_template('entry.html',
                           the_title=title,)


@app.route('/search0', methods=['POST'])
def do_search_0() -> 'html':
    key = request.form['key']
    keywords = request.form['target_address']
    title = '查询结果'


    results = geo.poi_keywords(key, keywords)
    log_request(request, results)
    return render_template('results.html',
                           the_results=results,
                           the_key=key,
                           the_title=title,
                           the_keywords=keywords,

                    )




@app.route('/viewlog')
def view_the_log() -> 'html':
    contents = []
    with open('target_address_poi.log', 'r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ('用户输入数据', '远程地址', '用户代理', '查询结果')
    return render_template('log.html',
                           the_title='查看日志',
                           the_row_titles=titles,
                           the_data=contents,)


if __name__ == '__main__':
    app.run(debug=True)
